<template>
  <div class="login-wrap">
    <div class="ms-login">
      <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="0px" class="demo-ruleForm">
        <div class="ms-title ivu-card-head">
          <p>管理员登录</p>
        </div>
        <el-form-item prop="username" class="ms-ipt">
          <el-input v-model="ruleForm.name" placeholder="username">
            <template slot="prepend">账号</template>
          </el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input type="password" placeholder="password" v-model="ruleForm.pwd"
                    @keyup.enter.native="submitForm('ruleForm')">
            <template slot="prepend">密码</template>
          </el-input>
        </el-form-item>
        <el-form-item v-show="show">
          <p style="color: red">密码或账号错误！</p>
        </el-form-item>
        <div class="login-btn">
          <el-button type="primary" @click="submitForm('ruleForm')">登录</el-button>
        </div>
      </el-form>
    </div>
  </div>
</template>

<script>
  export default {
    name: "login",
    data() {
      return {
        show: false,
        submitFormUrl: '',
        ruleForm: {
          name: '',
          pwd: '',
        },
        rules: {
          name: [
            {required: true, message: '请输入用户名', trigger: 'blur'}
          ],
          pwd: [
            {required: true, message: '请输入密码', trigger: 'blur'}
          ]
        }
      }
    },
    methods: {
      submitForm(formName) {
        this.$refs[formName].validate((valid) => {
          if (valid) {
            let _this = this;
            let formData = new FormData();
            formData.append("name", _this.ruleForm.name);
            formData.append("pwd", _this.$md5(_this.ruleForm.pwd));
            let config = {
              headers: {'Content-Type': 'multipart/form-data'}
            };
            _this.$http.httpPost('/user/logon', formData).then((res) => {
              if (res.data == true) {
                localStorage.setItem('ms_username', _this.ruleForm.name);
                _this.$router.push('/');
              } else {
                _this.show = true
              }
            });

          } else {
            return false;
          }
        });
      },
    }
  }
</script>

<style scoped>
  .login-wrap {
  }

  .ms-login {
    position: absolute;
    left: 50%;
    top: 40%;
    width: 300px;
    height: 235px;
    margin: -150px 0 0 -190px;
    padding: 20px 30px;
    border-radius: 5px;
    background: #fff;
  }

  .ms-title {
    font-size: 20px;
    font-weight: bold;
  }

  .ms-ipt {
    padding-top: 20px;
  }

  .login-btn {
    text-align: center;
  }

  .login-btn button {
    width: 100%;
    height: 36px;
  }
</style>
